package com.shanks.framework.mq.domain;

import com.fasterxml.jackson.annotation.JsonIgnoreType;
import com.shanks.framework.mq.exception.MQException;
import com.shanks.framework.mq.common.util.MQUtil;

import java.io.UnsupportedEncodingException;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 消息消费对象
 */
public class MQMessage {
    /**
     * 消息id
     */
    private String msgId;
    /**
     * 消息topic
     */
    private String topic;
    /**
     * 消息tag
     */
    private String tags;
    /**
     * 消息key
     */
    private String key;
    /**
     * 消息体
     */
    private byte[] body;

    /**
     * 原始消息对象
     */
    private Object originalMessage;
    /**
     * 消息属性
     */
    private Map<String, Object> properties;


    public MQMessage(String msgId, String topic, String tags, String key, byte[] body, Map<String, Object> properties) {
        this.msgId = msgId;
        this.topic = topic;
        this.tags = tags;
        this.key = key;
        this.body = body;
        this.properties = properties;
    }

    public MQMessage(String msgId, String topic, String tags, String key, byte[] body, Object originalMessage, Map<String, Object> properties) {
        this.msgId = msgId;
        this.topic = topic;
        this.tags = tags;
        this.key = key;
        this.body = body;
        this.originalMessage = originalMessage;
        this.properties = properties;
    }

    public String getMsgId() {
        return msgId;
    }

    public void setMsgId(String msgId) {
        this.msgId = msgId;
    }

    public String getTopic() {
        return topic;
    }

    public void setTopic(String topic) {
        this.topic = topic;
    }

    public String getTags() {
        return tags;
    }

    public void setTags(String tags) {
        this.tags = tags;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public byte[] getBody() {
        return body;
    }

    public void setBody(byte[] body) {
        this.body = body;
    }

    public Map<String, Object> getProperties() {
        return properties;
    }

    public void setProperties(Map<String, Object> properties) {
        this.properties = properties;
    }

    public String getBodyAsString() {
        try {
            return new String(body, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }

    @JsonIgnoreType
    public static class MQMessageBuilder {
        private transient Map<String, Object> messageData = new LinkedHashMap<>();

        public static MQMessageBuilder create() {
            return new MQMessageBuilder();
        }

        public MQMessageBuilder put(String name, Object value) {
            this.messageData.put(name, value);
            return this;
        }

        public MQMessageBuilder putAll(Map<String, Object> m) {
            this.messageData.putAll(m);
            return this;
        }

        public MQMessageBuilder remove(String name) {
            this.messageData.remove(name);
            return this;
        }

        public Map<String, Object> build() throws MQException {
            put("createTime", Long.valueOf(System.currentTimeMillis()));
            put("signature", MQUtil.createSignature(this.messageData));
            return this.messageData;
        }
    }
}
